package com.school.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.school.dao.HljtGradeClassDao;
import com.school.dao.HljtGradetimeDao;
import com.school.entity.HljtGradetime;
import com.school.entity.web.HljtGradeInfo;
import com.school.service.HljtGradetimeService;
import com.school.util.MapperToJson;
import com.school.util.RepMessage;
@Service
public class HljtGradetimeServiceImpl implements HljtGradetimeService{

	@Autowired
	private HljtGradetimeDao hljtGradetimeDao;
	@Autowired
	private HljtGradeClassDao oHljtGradeClassDao;
	@Override
	public List<HljtGradetime> findGradetime(Integer GradeId) {
		return hljtGradetimeDao.findGradetime(GradeId);
	}

	@Override
	public String findGradetimeAndGrade(String GradeId) {
		RepMessage oRepMessage=new RepMessage();
		oRepMessage.setFlag(true);
		try {
			if(GradeId!="" &GradeId!=null){
				List<HljtGradetime> tList=hljtGradetimeDao.findGradetime(Integer.parseInt(GradeId));
				oRepMessage.getObjMap().put("GradeTime",tList) ;
			}
			List<HljtGradeInfo> iList=oHljtGradeClassDao.findAllGrades();
			oRepMessage.getObjMap().put("GradetInfo",iList) ;
		} catch (Exception e) {
			oRepMessage.setFlag(false);	
		}
		return MapperToJson.toJson(oRepMessage);
	}

	@Override
	public String UpdateGradetimeByGrade(int GradeId, String strMessage) {
		
		List<String> list=new ArrayList<String>(java.util.Arrays.asList(strMessage.split(",")));
		List<HljtGradetime> hList=new ArrayList<HljtGradetime>();
		int section=1;
		for (int i = 0; i < list.size(); i++) {
			HljtGradetime oHljtGradetime=new HljtGradetime();
			oHljtGradetime.setGradeid(GradeId);
			oHljtGradetime.setSection(section);
			if(!"".equals(list.get(i))&&null!=list.get(i)) {
				oHljtGradetime.setId(Integer.parseInt(list.get(i)));
			}
			oHljtGradetime.setStarttime(list.get(i+1));
			oHljtGradetime.setEndtime(list.get(i+2));
			hList.add(oHljtGradetime);
			section++;
			i+=2;
		}
		RepMessage oRepMessage=new RepMessage();
		int nCount=	hljtGradetimeDao.selectCountByGradeId(GradeId);
		
		if(nCount==0){
			
			oRepMessage.setErrInfo("添加信息");
			hljtGradetimeDao.insertGradetimeByGrade(hList);
			
		}else{
			hljtGradetimeDao.UpdateGradetimeByGrade(hList);
			oRepMessage.setErrInfo("修改信息");
		}
		
		
		oRepMessage.setFlag(true);
		oRepMessage.getObjMap().put("GradeTime",hList) ;
		/*try {
		} catch (Exception e) {
			oRepMessage.setFlag(false);
		}*/
		return MapperToJson.toJson(oRepMessage);
	}

	@Override
	public List<HljtGradetime> getGradetime(Integer GradeId, Integer section) {
			return hljtGradetimeDao.getGradetime(GradeId, section);
	}

	@Override
	public Integer addGradetime(HljtGradetime hljtGradetime) {
		if(hljtGradetimeDao.addGradetime(hljtGradetime)>0) {
			return 1;
		}else {
			return 0;
		}
	}

	@Override
	public Integer updateGradetime(HljtGradetime hljtGradetime) {
		if(hljtGradetimeDao.updateGradetime(hljtGradetime)>0) {
			return 1;
		}else {
			return 0;
		}
	}

}
